/*
 * Copyright 2007 Rob Nielsen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.proxymatic.core;

import com.googlecode.proxymatic.core.handlers.exception.DeclaredExceptionHandler;
import com.googlecode.proxymatic.core.handlers.exception.RuntimeOrErrorExceptionHandler;
import com.googlecode.proxymatic.core.handlers.method.EqualsAndHashCodeMethodHandler;
import com.googlecode.proxymatic.core.handlers.method.ToStringMethodHandler;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class HandlerConfiguration implements Serializable {
    private List<MethodHandler> methodHandlers = new ArrayList<MethodHandler>();
    private List<ExceptionHandler> exceptionHandlers = new ArrayList<ExceptionHandler>();

    public HandlerConfiguration() {
    }

    public HandlerConfiguration(HandlerConfiguration handlerConfiguration) {
        methodHandlers.addAll(handlerConfiguration.getMethodHandlers());
        exceptionHandlers.addAll(handlerConfiguration.getExceptionHandlers());
    }

    public HandlerConfiguration addHandler(MethodHandler methodHandler) {
        methodHandlers.add(methodHandler);
        return this;
    }

    public HandlerConfiguration addHandler(ExceptionHandler exceptionHandler) {
        exceptionHandlers.add(exceptionHandler);
        return this;
    }

    public HandlerConfiguration removeHandlers(Class handlerClass) {
        if (MethodHandler.class.isAssignableFrom(handlerClass)) {
            remove(methodHandlers, handlerClass);
        } else if (ExceptionHandler.class.isAssignableFrom(handlerClass)) {
            remove(exceptionHandlers, handlerClass);
        }
        return this;
    }

    public List<MethodHandler> getMethodHandlers() {
        return methodHandlers;
    }

    public List<ExceptionHandler> getExceptionHandlers() {
        return exceptionHandlers;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        HandlerConfiguration that = (HandlerConfiguration) o;
        if (!exceptionHandlers.equals(that.exceptionHandlers)) {
            return false;
        }
        return methodHandlers.equals(that.methodHandlers);
    }

    public int hashCode() {
        int result;
        result = methodHandlers.hashCode();
        result = 31 * result + exceptionHandlers.hashCode();
        return result;
    }

    public String toString() {
        return "HandlerConfiguration(" + methodHandlers + ":" + exceptionHandlers + ")";
    }

    public void clearHandlers() {
        methodHandlers.clear();
        exceptionHandlers.clear();
    }

    private void remove(List handlers, Class handlerClass) {
        for (Iterator iterator = handlers.iterator(); iterator.hasNext();) {
            Object o = iterator.next();
            if (handlerClass.isAssignableFrom(o.getClass())) {
                iterator.remove();
            }
        }
    }

    public static HandlerConfiguration baselineConfiguration(String proxyName) {
        HandlerConfiguration handlerConfiguration = new HandlerConfiguration();
        handlerConfiguration.addHandler(new EqualsAndHashCodeMethodHandler());
        handlerConfiguration.addHandler(new ToStringMethodHandler(proxyName));
        handlerConfiguration.addHandler(new DeclaredExceptionHandler());
        handlerConfiguration.addHandler(new RuntimeOrErrorExceptionHandler());
        return handlerConfiguration;
    }
}
